import { Sprite, SpriteOptions } from './Sprite';
import { GameObject } from '../core/GameObject';
import { SpriteAnimation, SpriteData } from './types';

export interface AnimatedSpriteOptions extends SpriteOptions {
    /**
     * 初始动画名称
     */
    initialAnimation?: string;
    
    /**
     * 是否自动播放初始动画
     */
    autoPlay?: boolean;
}

export class AnimatedSprite extends Sprite {
    private animations: Map<string, SpriteAnimation>;
    private currentAnimation: SpriteAnimation | null;
    private animationTime: number;
    private currentFrameIndex: number;
    
    constructor(gameObject: GameObject, options: AnimatedSpriteOptions) {
        super(gameObject, options);
        
        // 初始化动画数据
        this.animations = new Map();
        this.currentAnimation = null;
        this.animationTime = 0;
        this.currentFrameIndex = 0;
        
        // 加载动画数据
        if (options.data.animations) {
            for (const animation of options.data.animations) {
                this.animations.set(animation.name, animation);
            }
        }
        
        // 设置初始动画
        if (options.initialAnimation) {
            this.playAnimation(options.initialAnimation, options.autoPlay ?? true);
        } else if (options.data.defaultAnimation) {
            this.playAnimation(options.data.defaultAnimation, options.autoPlay ?? true);
        }
    }
    
    /**
     * 播放指定动画
     */
    public playAnimation(name: string, autoPlay: boolean = true): void {
        const animation = this.animations.get(name);
        if (!animation) {
            console.warn(`Animation "${name}" not found`);
            return;
        }
        
        this.currentAnimation = animation;
        this.currentFrameIndex = 0;
        this.animationTime = 0;
        
        // 设置第一帧
        const frame = animation.frames[0];
        this.setFrame(frame);
        
        // 设置播放状态
        this.setIsPlaying(autoPlay);
    }
    
    /**
     * 停止当前动画
     */
    public stopAnimation(): void {
        this.setIsPlaying(false);
        this.animationTime = 0;
    }
    
    /**
     * 获取当前动画名称
     */
    public getCurrentAnimation(): string | null {
        return this.currentAnimation?.name ?? null;
    }
    
    /**
     * 获取动画列表
     */
    public getAnimationNames(): string[] {
        return Array.from(this.animations.keys());
    }
    
    /**
     * 检查动画是否存在
     */
    public hasAnimation(name: string): boolean {
        return this.animations.has(name);
    }
    
    /**
     * 更新组件
     */
    public update(deltaTime: number): void {
        if (!this.getIsPlaying() || !this.getVisible() || !this.currentAnimation) {
            return;
        }
        
        // 更新动画时间
        this.animationTime += deltaTime * (this.currentAnimation.speed ?? 1) * this.getSpeed();
        
        // 获取当前帧的持续时间
        const currentFrame = this.currentAnimation.frames[this.currentFrameIndex];
        const frameDuration = currentFrame.duration ?? 100;
        
        // 检查是否需要切换到下一帧
        if (this.animationTime >= frameDuration) {
            this.animationTime = 0;
            this.currentFrameIndex++;
            
            // 检查是否需要循环或结束动画
            if (this.currentFrameIndex >= this.currentAnimation.frames.length) {
                if (this.currentAnimation.loop ?? true) {
                    this.currentFrameIndex = 0;
                } else {
                    this.currentFrameIndex = this.currentAnimation.frames.length - 1;
                    this.setIsPlaying(false);
                }
            }
            
            // 设置新帧
            const frame = this.currentAnimation.frames[this.currentFrameIndex];
            this.setFrame(frame);
        }
    }
    
    /**
     * 销毁组件
     */
    public destroy(): void {
        this.animations.clear();
        this.currentAnimation = null;
        super.destroy();
    }
} 